from sklearn.preprocessing import OneHotEncoder
from sklearn.tree import DecisionTreeClassifier

dataset = {
    'data': [['Sunny', 'Hot', 'High', 'Weak'], ['Sunny', 'Hot', 'High', 'Strong'],
             ['Overcast', 'Hot', 'High', 'Weak'], ['Rain', 'Mild', 'High', 'Weak'],
             ['Rain', 'Cool', 'Normal', 'Weak'], ['Rain', 'Cool', 'Normal', 'Strong'],
             ['Overcast', 'Cool', 'Normal', 'Strong'], ['Sunny', 'Mild', 'High', 'Weak'],
             ['Sunny', 'Cool', 'Normal', 'Weak'], ['Rain', 'Mild', 'Normal', 'Weak'],
             ['Sunny', 'Mild', 'Normal', 'Strong'], ['Overcast', 'Mild', 'High', 'Strong'],
             ['Overcast', 'Hot', 'Normal', 'Weak'], ['Rain', 'Mild', 'High', 'Strong']],
    'target': ['No', 'No', 'Yes', 'Yes', 'Yes', 'No', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 'Yes', 'Yes', 'No']
}

if __name__ == '__main__':
    onehot_encoder = OneHotEncoder()
    features_train = dataset['data']
    labels_train = dataset['target']
    features_train_encoded = onehot_encoder.fit_transform(features_train).toarray()

    decision_tree_classifier = DecisionTreeClassifier(criterion='entropy')
    decision_tree_classifier.fit(features_train_encoded, labels_train)

    features_test = [['Rain', 'Hot', 'High', 'Weak']]
    features_test_encoded = onehot_encoder.transform(features_test).toarray()
    test_label = decision_tree_classifier.predict(features_test_encoded)

    accuracy = decision_tree_classifier.score(features_train_encoded, labels_train)
    print("预测准确率:", accuracy)
    print("预测结果:", test_label)
